package com.ruoyi.report.util;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.report.domain.ReportParam;
import com.ruoyi.report.domain.ReportShowtable;
import com.ruoyi.report.domain.ReportShowtableColumn;
import com.ruoyi.report.mapper.ReportShowtableColumnMapper;
import com.ruoyi.report.service.IReportParamService;
import com.ruoyi.report.service.IReportShowtableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/***
 * 报告生成类
 */
@Component
public class ReportUtil {
    @Autowired
    private  IReportParamService paramService;
    @Autowired
    private IReportShowtableService showTableService;
    @Autowired
    private ReportShowtableColumnMapper reportShowtableColumnMapper;
    public static ReportUtil reportUtil;

    @PostConstruct
    public void init() {
        reportUtil = this;
        reportUtil.paramService = this.paramService;
        reportUtil.showTableService = this.showTableService;
        reportUtil.reportShowtableColumnMapper = this.reportShowtableColumnMapper;
    }

    public  void doCreateReport(String reportTpl,String reportPath){
        try {
            FileUtils.copyDir(reportTpl,reportPath);
            File reportFile = new File(reportPath+"template.html");
            if(reportFile.exists()){
                //读取文件
                String content = org.apache.commons.io.FileUtils.readFileToString(reportFile, "utf-8");
                List<ReportParam> paramList=reportUtil.paramService.selectReportParamList(null);
                for (int i = 0; i < paramList.size(); i++) {
                    ReportParam param=paramList.get(i);
                    if("1".equals(param.getParamType())){//直接替换文本
                        content = content.replaceAll("\\$"+param.getParamName()+"\\$",param.getParamName()+"test");
                    }else if("2".equals(param.getParamType())){//日期格式化后替换
                        String timeformat=DateUtils.YYYY_MM_DD;
                        if(StringUtils.isNotEmpty(param.getParamFormat())){
                            timeformat=param.getParamFormat();
                        }
                        content = content.replaceAll("\\$"+param.getParamName()+"\\$", DateUtils.parseDateToStr(timeformat,DateUtils.parseDate("2023-06-08 12:12:12")));
                    }else if("3".equals(param.getParamType())){//当前时间格式化替换
                        String timeformat=DateUtils.YYYY_MM_DD_HH_MM_SS;
                        if(StringUtils.isNotEmpty(param.getParamFormat())){
                            timeformat=param.getParamFormat();
                        }
                        content = content.replaceAll("\\$"+param.getParamName()+"\\$", DateUtils.parseDateToStr(timeformat,new Date()));
                    }else if("5".equals(param.getParamType())){//检测类型自定义
                        content = content.replaceAll("\\$"+param.getParamName()+"\\$", doGetAssValue(param.getParamKey(),param.getParamDefault(),"琼脂糖凝胶电泳定量"));
                    }else if("6".equals(param.getParamType())){//图片展示
                        content = content.replaceAll("\\$"+param.getParamName()+"\\$", doSetImage(reportPath+param.getParamDefault()));
                    }
                }
                List<ReportShowtable> showList=reportUtil.showTableService.selectReportShowtableList(new ReportShowtable());
                for (int i = 0; i < showList.size(); i++) {
                    ReportShowtable t=showList.get(i);
                    System.out.println("$"+t.getReportTableName()+"$");
                    System.out.println(content.contains("$"+t.getReportTableName()+"$"));
                    if(content.contains("$"+t.getReportTableName()+"$")){
                        int columnnum=1;
                        StringBuffer sb=new StringBuffer();
                        for (int j = 0; j < 5; j++) {
                            sb.append("<tr><td>"+columnnum+"</td>");
                            sb.append(doGetTrValue(t.getTableId().toString(),t.getReportTableName()));
                            columnnum=columnnum+1;
                        }
                        content = content.replaceAll("\\$"+t.getReportTableName()+"\\$", sb.toString());
                    }
                }

                // 写入
                OutputStream fos = new FileOutputStream(reportFile);
                fos.write(content.getBytes("UTF-8"));

                //关流
                fos.flush();
                fos.close();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public String doGetTrValue(String tableId,String tableName){
        StringBuffer sb=new StringBuffer();
        List<ReportShowtableColumn> columnList= reportUtil.reportShowtableColumnMapper.selectReportShowtableColumnByTableId(tableId,tableName);
        for (int j = 0; j < columnList.size(); j++) {
            ReportShowtableColumn column=columnList.get(j);
            sb.append("<td>test"+j+"</td>");
        }
        sb.append("</tr>");
        return sb.toString();
    }

    public String doSetImage(String path){
        List<File> allFileList=FileUtils.getAllFile(path);
        StringBuffer sb=new StringBuffer();
        for (File file : allFileList) {
            sb.append(file.getName()+"<li>").append("<a id=\"example2\" href='src/Caliper/"+file.getName()+"'>")
                    .append("<img src='src/Caliper/"+file.getName()+"' /></a>");
        }
       return sb.toString();
    }

    /***
     * 自定义替换
     * @param key
     * @param paramDefault 文本默认值
     * @param changeValue  需要替换的文本
     * @return
     */
    public String doGetAssValue(String key,String paramDefault,String changeValue) {
        if ("test".equals(key)&& StringUtils.isNotEmpty(changeValue)) {
            System.out.println( paramDefault.replaceAll("□ " + changeValue + " ", "<strong>√</strong>" + changeValue + " "));
            return paramDefault.replaceAll("□ " + changeValue + " ", "<strong>√ </strong>" + changeValue + " ");
        }
        return paramDefault;
    }


}
